﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace MedVis
{
    public class ImageStack
    {
        private static ImageStack instance = null;
        private int currentImage = 0;
        //private FileIO fio = FileIO.GetInstance;
        //xml data
        public double stackWidth = 0;
        public double stackHeight = 0;
        public double sliceThickness = 0;
        public int cropTop = 0;
        public int cropLeft = 0;
        public int cropBot = 0;
        public int cropRight = 0;
        public double opacityPrev = 1;
        public double opacityCurr = 1;
        public double opacityNext = 1;
        public ObservableCollection<MyImageObject> imageObjects = new ObservableCollection<MyImageObject>();

        private ImageStack()
        {
        }

        public static ImageStack GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ImageStack();
                }
                return instance;
            }
        }

        public void loadImages(ObservableCollection<String> imagePath)
        {
            imageObjects.Clear();
            currentImage = 0;
            for (int i = 0; i <= (imagePath.Count - 1); i++)
            {
                //MyImageObject newImage = new MyImageObject(imagePath[i]);
                imageObjects.Add(new MyImageObject(imagePath[i]));//newImage);
            }

            //imagePath.Clear();
            //imagePath = null;
        }

        public void setImageCollection(ObservableCollection<MyImageObject> list)
        {
            imageObjects.Clear();
            imageObjects = new ObservableCollection<MyImageObject>(list);
        }

        public void loadFolder()
        {
            imageObjects.Clear();
            List<String> imagePath = new List<String>();
            //fio.LoadImagesDirectory();
            //imagePath = fio.ImagePath;

            for (int i = 0; i <= (imagePath.Count - 1); i++)
            {
                MyImageObject newImage = new MyImageObject(imagePath[i]);
                imageObjects.Add(newImage);
            }

            imagePath.Clear();
            imagePath = null;
        }
        public MyImageObject getNextImage()
        {
            if ((currentImage + 1) <= (imageObjects.Count - 1))
            {
                currentImage += 1;
            }
            else
            {
                currentImage = 0;
            }

            return imageObjects[currentImage];
        }

        public MyImageObject getPreviusImage()
        {
            if ((currentImage - 1) >= 0)
            {
                currentImage -= 1;
            }
            else
            {
                currentImage = (imageObjects.Count - 1);
            }

            return imageObjects[currentImage];
        }

        public MyImageObject getImage(int imageNummber)
        {
            if (imageNummber <= (imageObjects.Count - 1) && imageNummber >= 0)
            {
                currentImage = imageNummber;
            }
            else
            {
                return null;
            }

            return imageObjects[currentImage];
        }

        public ObservableCollection<MyImageObject> getAllImages()
        {
            return imageObjects;
        }

        public MyImageObject getCurrentImage()
        {
            return imageObjects[currentImage];
        }

        public void reset()
        {
            currentImage = 0;
            stackWidth = 0;
            stackHeight = 0;
            sliceThickness = 1;
            cropTop = 0;
            cropLeft = 0;
            cropBot = 0;
            cropRight = 0;
            imageObjects.Clear();
        }
    }

}
